//
//  NSMNetworkHelper.h
//  NSMKit
//
//  Created by nsm on 2019/3/1.
//  Copyright © 2019 nani. All rights reserved.
//

#import <Foundation/Foundation.h>

#define _networker      ([NSMNetworkHelper sharedInstance])

@interface NSMResponseObject : NSObject
@property (nonatomic) NSInteger code;
@property (nonatomic, copy) NSString *desc;
@property (nonatomic, strong) id data;
@property (nonatomic, copy) NSString *url;
@property (nonatomic, strong) NSDictionary *parameters;
@end

@class AFHTTPSessionManager;
@protocol AFMultipartFormData;
// 若根目录下server.cer文件存在则代表启用Https证书验证
@interface NSMNetworkHelper : NSObject
@property (nonatomic, strong, readonly) AFHTTPSessionManager *manager;
/** 是否打印输出网络请求,默认NO */
@property (nonatomic, copy) BOOL (^shouldPrintRequest)(void);
/** AFHTTPSessionManager的baseURL */
@property (nonatomic, copy) NSString *(^baseURL)(void);
/** 是否采用body方式传输参数,默认NO */
@property (nonatomic, copy) BOOL (^shouldRequestParametersInBody)(NSString *URLString, NSDictionary *parameters);
/**
 是否允许网络被代理,默认允许
 parameters 最终网络请求参数
 若返回YES,则过滤网络请求,触发failure(error_code=600)和didFailLoad(nil, error_code=600)回调
 */
@property (nonatomic, copy) BOOL(^proxyEnabled)(NSDictionary *parameters);
/**
 最终网络请求的url,可在此block中拼接完整URL
 URLString 实例方法中的URLString
 !!如果URLString是http开头不做任何处理
 */
@property (nonatomic, copy) NSString *(^finalURLString)(NSString *URLString);
/**
 最终网络请求的参数,可在此block中添加默认参数,接口加密
 parameters 实例方法中的parameters
 */
@property (nonatomic, copy) NSDictionary *(^finalParameters)(NSDictionary *parameters);
/**
 网络请求出错的全局处理方式,x:0/1/2...
 除了接口返回的code码20x/30x 以外均视为出错
 error 为nil代表接口返回的错误,比如code:40x,50x等,根据responseObject处理
 error 非nil代表系统错误,比如无网络,代理被拒绝,请求失败,此时responseObject为nil
 */
@property (nonatomic, copy) void(^didFailLoad)(NSMResponseObject *responseObject, NSError *error);
/** 准备网路请求的时候,自行进行加载动画显示 */
@property (nonatomic, copy) void(^willStartLoad)(BOOL isShowHud);
/** 结束网路请求的时候,自行进行加载动画隐藏 */
@property (nonatomic, copy) void(^didFinishLoad)(BOOL isShowHud);
/** 接口超时 */
@property (nonatomic, copy) NSTimeInterval(^timeoutInterval)(NSString *URLString, NSDictionary *parameters);


+ (instancetype)sharedInstance;

- (NSURLSessionDataTask *)POST:(NSString *)URLString
                    parameters:(NSDictionary *)parameters
                       success:(void (^)(NSMResponseObject *responseObject))success
                         empty:(void (^)(NSMResponseObject *responseObject))empty
                       failure:(void (^)(NSError *error))failure;

- (NSURLSessionDataTask *)POST:(NSString *)URLString
                    parameters:(NSDictionary *)parameters
                       success:(void (^)(NSMResponseObject *responseObject))success
                         empty:(void (^)(NSMResponseObject *responseObject))empty
                       failure:(void (^)(NSError *error))failure
                           hud:(BOOL)isShowHud;

- (NSURLSessionDataTask *)POST:(NSString *)URLString
                    parameters:(NSDictionary *)parameters
                 formDataBlock:(void (^)(id <AFMultipartFormData> formData))block
                       success:(void (^)(NSMResponseObject *responseObject))success
                         empty:(void (^)(NSMResponseObject *responseObject))empty
                       failure:(void (^)(NSError *error))failure;

- (NSURLSessionDataTask *)POST:(NSString *)URLString
                    parameters:(NSDictionary *)parameters
                 formDataBlock:(void (^)(id <AFMultipartFormData> formData))block
                       success:(void (^)(NSMResponseObject *responseObject))success
                         empty:(void (^)(NSMResponseObject *responseObject))empty
                       failure:(void (^)(NSError *error))failure
                           hud:(BOOL)isShowHud;
- (NSURLSessionDataTask *)POST:(NSString *)URLString
                    parameters:(NSDictionary *)parameters
                 formDataBlock:(void (^)(id <AFMultipartFormData> formData))block
                      progress:(void (^)(NSProgress *uploadProgress))uploadProgress
                       success:(void (^)(NSMResponseObject *responseObject))success
                         empty:(void (^)(NSMResponseObject *responseObject))empty
                       failure:(void (^)(NSError *error))failure
                           hud:(BOOL)isShowHud;
@end
